package com.ziyia.a272;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TableRow;
import android.widget.ViewSwitcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    ImageSwitcher switcher;
    private int index;
    int[] imageIds = new int[]{R.drawable.bomb6,
            R.drawable.bomb7, R.drawable.bomb8, R.drawable.bomb9,
            R.drawable.bomb10, R.drawable.bomb11, R.drawable.bomb12,
            R.drawable.bomb13, R.drawable.bomb14, R.drawable.bomb15,
            R.drawable.bomb16
    };
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 创建一个List对象，List对象的元素是Map
        List<Map<String, Object>> listItems = new ArrayList();
        for (int i = 0; i < imageIds.length; i++)
        {
            Map<String, Object> listItem = new HashMap<>();
            listItem.put("image", imageIds[i]);
            listItems.add(listItem);
        }


        // 获取显示图片的ImageSwitcher
        switcher = findViewById(R.id.imageSwitch);


        // 为ImageSwitcher设置图片切换的动画效果
        // 使用Lambda表达式创建ViewFactory，表达式是makeView方法()的方法体
        switcher.setFactory(() -> {
            // 创建ImageView对象
            ImageView imageView = new ImageView(MainActivity.this);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT));
            // 返回ImageView对象
            return imageView;
        });
        // 创建一个SimpleAdapter
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems,
                R.layout.cell,  // 使用/layout/cell.xml文件作为界面布局
                new String[]{"image"}, new int[]{R.id.image1});
        GridView grid = findViewById(R.id.grid);
        // 为GridView设置Adapter
        grid.setAdapter(simpleAdapter);
        // 添加列表项被选中的监听器
        grid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                // 显示当前被选中的图片
                switcher.setImageResource(imageIds[position]);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
            }
        });
        // 添加列表项被单击的监听器
        grid.setOnItemClickListener((parent, view, position, id) -> {
            // 显示被单击的图片
            switcher.setImageResource(imageIds[position]);
        });
    }

    public void next(View view) {
        switcher.setImageResource(imageIds[index++ % imageIds.length]);
    }

    public void prev(View view) {
        switcher.setImageResource(imageIds[index-- % imageIds.length]);
    }
}